;(function () {
  new Vue({
    el: '#JYprint',
    data: {
      data: [],
      projectData: {
        companyName: '',
        name: '',
        date:'',
        bankAccounts: ''
      },
      num: 0,
      contractData: [],
      units:null,
      amountData: {
        expenses: []
      },
      rentPriceUnit:'D'
    },
    filters: {
      dateCover: function (time) {
        return hlDateUtil.formatUTCDate(time,'yyyy-MM-dd')
      },
      toFixed2: function (n) {
        return Number(n).toFixed(2)
      }
    },
    computed: {
      totalAmountTPay: function () {
        var result = 0;
        this.amountData.expenses.map(function(item) {
          result+=item.amountTPay
        })

        return result
      },
      totalRefundTPay: function () {
        var result = 0;
        this.amountData.expenses.map(function(item) {
          result+=item.refundTPay
        })

        return result
      },
    },
    methods: {
      //联动银行
      linkBank: function (i) {
        this.num = i;
      },
      //获取项目
      getProjectMes: function () {
        var projectId = window.localStorage.getItem('PROJECTID');
        var _this = this;
        hlhttp.get({
          url: 'project',
          data: {project_id:projectId},
          success: function (res) {
            _this.projectData = {
              companyName: res.pcompany,
              name: res.name,
              bankAccounts: res.bankAccounts ? res.bankAccounts.split(',') : null,
              bankName: res.bankNames ? res.bankNames.split(',') : null,
              accountNames: res.accountNames ? res.accountNames.split(',') : null,
              date: hlDateUtil.formatUTCDate(new Date().getTime(),'yyyy-MM-dd'),
              legalName: res.legalName,
              addrees: res.provinceName+' '+res.cityName+ ' ' + res.districtName+' '+res.address,
              rpersonPhone: res.rpersonPhone,
              rpersonName: res.rpersonName
            }
          }
        })
      },
      //获取合同信息
      getContractMes: function () {
        var id = hlUrlUtil.GetQueryString('aid');
        var self = this;
        hlhttp.get({
          url: 'contract',
          data: {id:id},
          success: function (res) {
            self.rentPriceUnit = res.rentPriceUnit;
            self.contractData = {
              projectName:res.projectName,
              units: res.units,
              zlfCompany: res.zlfCompany,
              acreage: res.rentAcreage,
              rentPrice: res.rentPrice
            }

            for (var i = 0; i <  self.amountData.expenses.length; i++) {
              for (var j = 0; j < res.expenses.length; j++) {
                if (self.amountData.expenses[i].name === res.expenses[j].expenseName) {
                  self.amountData.expenses[i].expense = res.expenses[j].expense
                }
              }
            }

            // 调整单元的数据
            var unitsArr = [];
            res.units.map(function(_item){
              var obj = {}
              if(_item.positions.length == 0){
                obj = {
                  'buildingName':_item.buildingName,
                  'floorNum':_item.floorNum,
                  'unitName':_item.unitName,
                  'positionName':''
                }
                unitsArr.push(obj)
              }
              if(_item.positions.length){
                // 工位类型
                if(_item.unitType === 'WP'){
                  obj = {
                    'buildingName':_item.buildingName,
                    'floorNum':_item.floorNum,
                    'unitName':_item.unitName,
                    'positionName':'工位' + _item.positions.length + '个'
                  }
                  unitsArr.push(obj)
                }
                // 车位类型
                if(_item.unitType === 'ST'){
                  obj = {
                    'buildingName':_item.buildingName,
                    'floorNum':_item.floorNum,
                    'unitName':_item.unitName,
                    'positionName':'车位' + _item.positions.length + '个'
                  }
                  unitsArr.push(obj)
                }
              }
            })
            self.units = unitsArr
          }
        })
      },
      //获取缴费信息
      getAccountsMes: function () {
        var id = hlUrlUtil.GetQueryString('id');
        var _this = this;
        hlhttp.get({
          url: 'bill,expenses,transactions',
          data: {bill_id: id},
          success: function (res) {
            console.log(res);

            _this.amountData = {
              endDate: hlDateUtil.formatUTCDate(res.deadlineDate,'yyyy-MM-dd'),
              expenses: res.expenses,
              beginTime: res.beginDate,
              endTime: res.endDate
            }
            _this.getContractMes()
          }
        })
      },
      //打印
      print: function () {
        $(document).on('click', '.js-print', function(event) {
          var bdhtml=window.document.body.innerHTML;
          var prnhtml = $('.pt-wrap').html()
          window.document.body.innerHTML=prnhtml;
          window.print();
          window.document.body.innerHTML=bdhtml;
        });
      },
      // 导出PDF
      exportPDF:function(){
        var doctypeHtml = '<!DOCTYPE html>' +
              '<html lang="en">' +
              '<head>' +
                '<meta charset="UTF-8"></meta>' +
                '<link rel="stylesheet" href="/opt/haolou/wbuild-page-saas-pc/com.fenzhitech.haolou-saas/wbuild-saas-pc/styles/libs/bootstrap_3.3.7.css"></link>' +
                '<link rel="stylesheet" href="/opt/haolou/wbuild-page-saas-pc/com.fenzhitech.haolou-saas/wbuild-saas-pc/styles/libs/font.css"></link>' +
                '<link rel="stylesheet" href="/opt/haolou/wbuild-page-saas-pc/com.fenzhitech.haolou-saas/wbuild-saas-pc/styles/app/blue/blue.min.css"></link>' +
              '</head>'+
              '<body>'

        $(document).on('click', '.export-pdf', function(event) {
          var ctxHtml = $('.pt-wrap').html()
          var modelHtml = doctypeHtml + ctxHtml + '</body></html>'
          // modelHtml = modelHtml.replace('http://s.1hello.cn/wbuild-saas-pc','/opt/haolou/wbuild-page-saas-pc/com.fenzhitech.haolou-saas/wbuild-saas-pc')

          $.ajax({
            url:convert2pdfApi,
            type: 'post',
            data:{'html_str':modelHtml},
            headers:{token:Cookies.get("token")},
            success:function(data){
              window.open(data.url)
            }
          })
        })
      },
      //as
      tdhover: function () {
        var oinput;
        $('td[data-focus=true]').on('focus', 'li', function(event) {
          oinput = $(this).text()
        });
        $('td[data-focus=true]').on('blur', 'li', function(event) {
          if ($(this).text() == '') {
            $(this).text(oinput)
          }
        });
      }

    },
    mounted: function () {
      hlSelectUi()
      this.getProjectMes()
      this.getAccountsMes()
      this.print()
      this.exportPDF()
    }
  })
}())
